//-----------------------------------------------------------------------
// <copyright file="YeuCauDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class YeuCauDAOBase
    {
        #region Common methods
        public virtual YeuCau CreateYeuCauFromReader(IDataReader reader)
        {
            YeuCau item = new YeuCau();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("Mobile"))) item.Mobile = (string)reader["Mobile"];
                if (!reader.IsDBNull(reader.GetOrdinal("MaYeuCau"))) item.MaYeuCau = (int)reader["MaYeuCau"];
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDungYeuCau"))) item.NoiDungYeuCau = (string)reader["NoiDungYeuCau"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateYeuCauFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateYeuCau methods
            
        public virtual void CreateYeuCau(YeuCau yeuCau)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spYeuCauInsert");
                
				database.AddInParameter(dbCommand, "@Mobile", DbType.String, yeuCau.Mobile);
				database.AddInParameter(dbCommand, "@MaYeuCau", DbType.Int32, yeuCau.MaYeuCau);
				database.AddInParameter(dbCommand, "@NoiDungYeuCau", DbType.String, yeuCau.NoiDungYeuCau);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateYeuCauException, ex);
            }
        }

        #endregion
		
        #region UpdateYeuCau methods
        
        public virtual void UpdateYeuCau(YeuCau yeuCau)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spYeuCauUpdate");            
                
                database.AddInParameter(dbCommand, "@Mobile", DbType.String, yeuCau.Mobile);
                database.AddInParameter(dbCommand, "@MaYeuCau", DbType.Int32, yeuCau.MaYeuCau);
                database.AddInParameter(dbCommand, "@NoiDungYeuCau", DbType.String, yeuCau.NoiDungYeuCau);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateYeuCauException, ex);
            }
        }
        
        #endregion
		
        #region DeleteYeuCau methods
        public virtual void DeleteYeuCau(int maYeuCau)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spYeuCauDelete");
                
                database.AddInParameter(dbCommand, "@MaYeuCau", DbType.Int32, maYeuCau);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteYeuCauException, ex);
            }
        }

        #endregion
		
        #region GetYeuCau methods
        
        public virtual YeuCau GetYeuCau(int maYeuCau)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spYeuCauGet");
                
                database.AddInParameter(dbCommand, "@MaYeuCau", DbType.Int32, maYeuCau);
                
                YeuCau yeuCau =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        yeuCau = CreateYeuCauFromReader(reader);
                        reader.Close();
                    }
                }
                return yeuCau;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetYeuCauException, ex);
            }
        }
        
        #endregion
		
        #region GetYeuCauList methods
        public virtual YeuCauCollection GetYeuCauList(YeuCauColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spYeuCauGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                YeuCauCollection yeuCauCollection = new YeuCauCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        YeuCau yeuCau = CreateYeuCauFromReader(reader);
                        yeuCauCollection.Add(yeuCau);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return yeuCauCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetYeuCauListException, ex);
            }
        }
        
        public virtual YeuCauCollection GetYeuCauList(YeuCauColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetYeuCauList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}